package com.txby.platformApi.component.web;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.Constant;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformCore.entity.ProductDeviceSales;

/**
 * 产品设备所属信息
 */
@RestController
@RequestMapping("/pro/prodevice")
public class ProductDeviceSalesController  extends CommonController {
	
	/**
	 * 所有用户列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("pro:prodevice:list")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="SELECT t.*,t2.name deptName FROM  "+this.mBaseDao.tableName(ProductDeviceSales.class)+" t,sys_dept t2 WHERE t.deptId=t2.dept_id  " ;
		if(getUserId() != Constant.SUPER_ADMIN){
//			params.put("createUserId", getUserId());
			 join=getJoinSqlBracket();
			 sql+=" and t.deptId in "+getJoinSqlBracket();
		}
		String deviceSN=params.get("deviceSN")==null?"":params.get("deviceSN").toString();
		if(!ChkUtil.isEmptyAllObject(deviceSN)){
			if (deviceSN.contains("%") || deviceSN.contains("_")) {
				deviceSN = deviceSN.trim().replace("%", "\\%").replace("_", "\\_");
				deviceSN = "%" + deviceSN + "%";
				sql += " and t.deviceSN like #{pojo.deviceSN} escape '\\'";
				params.put("deviceSN", "%" + deviceSN.trim() + "%");
			} else {
				deviceSN = "%" + deviceSN.trim() + "%";
				sql += " and t.deviceSN like #{pojo.deviceSN} ";
				params.put("deviceSN", "%" + deviceSN.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by t.updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
 
	
	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("pro:prodevice:info")
	public R info(@PathVariable("id") Long id){
		String sql="SELECT t.*,t2.name deptName FROM  "+this.mBaseDao.tableName(ProductDeviceSales.class)+" t,sys_dept t2 WHERE t.deptId=t2.dept_id and id_="+id+"" ;
		Map prodevice =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("prodevice", prodevice);
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@RequestMapping("/save")
	@RequiresPermissions("pro:prodevice:save")
	public R save(@RequestBody ProductDeviceSales prodevice){
		ValidatorUtils.validateEntity(prodevice, AddGroup.class);
		String sql="select * from "+this.mBaseDao.tableName(ProductDeviceSales.class)+" where deviceSN='"+prodevice.getDeviceSN()+"'";
		ProductDeviceSales old=(ProductDeviceSales) this.mBaseDao.findBySqlUniqueObject(sql, ProductDeviceSales.class, null);
		 if(old!=null){
			 return R.ok().error("设备号重复");
		 }
		 prodevice.setCreater(getUserId().toString());
		 prodevice.setUpdater(getUserId().toString());
			this.mBaseDao.saveObject(prodevice);
		 
		return R.ok();
	}
	
/*	*//**
	 * 修改用户
	 *//*
	@SysLog("修改用户")
	@RequestMapping("/update")
	@RequiresPermissions("pro:prodevice:update")
	public R update(@RequestBody ProductDeviceSales prodevice){
		ValidatorUtils.validateEntity(prodevice, UpdateGroup.class);
		String sql="select * from "+this.mBaseDao.tableName(ProductDeviceSales.class)+" where id_='"+prodevice.getId_()+"'";
		ProductDeviceSales old=(ProductDeviceSales) this.mBaseDao.findBySqlUniqueObject(sql, ProductDeviceInit.class, null);
		if(old!=null){
	     old.setUpdater(getUserId().toString());
	     old.setUpdateTime(prodevice.getUpdateTime());
	     old.setName(prodevice.getName());
	     old.setDeviceSN(prodevice.getDeviceSN());
	     old.setLinkMan(prodevice.getLinkMan());
	     old.setSaleTime(prodevice.getSaleTime());
	     this.mBaseDao.updateObject(old);
		}else{
			prodevice.setCreater(getUserId().toString());
			prodevice.setUpdater(getUserId().toString());
			this.mBaseDao.saveObject(prodevice);
		}
		return R.ok();
	}
	*/
	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@RequestMapping("/update")
	@RequiresPermissions("pro:prodevice:update")
	public R update(@RequestBody  ProductDeviceSales prodevice){
		ValidatorUtils.validateEntity(prodevice, AddGroup.class);
		   String sql="select * from "+this.mBaseDao.tableName(ProductDeviceSales.class)+" where deviceSN ="+prodevice.getDeviceSN()+" and id_!="+prodevice.getId_()+" ";
		 //  String sql="select * from "+this.mBaseDao.tableName(ProductDeviceSales.class)+" where id_="+prodevice.getId_()+" and id_!="+prodevice.getId_()+" ";
		   ProductDeviceSales old=(ProductDeviceSales) this.mBaseDao.findBySqlUniqueObject(sql, ProductDeviceSales.class, null);
		 if(old!=null){
			 return R.ok().error("设备号重复");
		 }else{
			 old=(ProductDeviceSales) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(ProductDeviceSales.class)+" where   id_="+prodevice.getId_(), ProductDeviceSales.class, null);
			 if(old!=null){
				 this.mBaseDao.updateObject(prodevice);
			 }
		 }
		return R.ok();
	}
	
	
	
	
	
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@RequestMapping("/delete")
	@RequiresPermissions("pro:prodevice:delete")
	public R delete(@RequestBody Long[] ids){
		 for(Long id:ids){
		    	this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(ProductDeviceSales.class)+" where id_="+id+"", null);
		  }
			return R.ok();
	}
}
